package com.example.ccsystem.controller;

import com.example.ccsystem.entity.Student;
import com.example.ccsystem.service.StudentService;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Student")
public class StudentController {
    @Autowired
    private StudentService service;

    @ApiOperation(value="获取学生总数",notes = "")
    @RequestMapping(value = "/getAllStudents",method = RequestMethod.GET)
    public String getAllStudents(){
        return service.getAllStudents();
    }

    @ApiOperation(value="获取学生列表",notes="")
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public List<Map<String,Object>> findAllStudents(){
        List<Map<String,Object>> list=service.findAllStudent();
        return list;
    }

    @ApiOperation(value="获取学生",notes = "根据学生id获取学生信息")
    @RequestMapping(value = "/{sid}",method = RequestMethod.GET)
    public Student getBySId(@PathVariable int sid){
        Student student=service.getBySId(sid);
        return student;
    }

    @ApiOperation(value="添加学生",notes="添加学生")
    @RequestMapping(value = "/",method = RequestMethod.POST)
    public int addStu(Student student){
        int res=service.addStu(student);
        return res;
    }

    @ApiOperation(value="删除学生",notes = "根据学生学号删除学生")
    @RequestMapping(value="/{sid}",method = RequestMethod.DELETE)
    public int deleteStu(@PathVariable int sid){
        System.out.println(sid);
        int res=service.deleteStu(sid);
        return res;
    }

    @ApiOperation(value="修改学生信息",notes = "根据学生学号修改学生信息")
    @RequestMapping(value = "/",method = RequestMethod.PUT)
    public int updateStu(Student student){
        System.out.println(student.getSid());
        int isHas=service.isHasStu(student.getSid());
        int res=0;
        if(isHas==1){
            res=service.updateStu(student);
        }
        return res;
    }
}
